package com.wfm.bytebuffer;

import java.nio.ByteBuffer;

public class Demo {
    public static void main(String[] args) throws InterruptedException {
//        test();
        duplicateTest2();
    }

    private static void test() {
        // 用ByteBuffer分配一个直接缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        System.out.println("初始化："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        // 像往缓冲区中写数据
        byteBuffer.put("Hello World!".getBytes());
        System.out.println("写完hello world!："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        // 读取缓冲区中的数据
        byteBuffer.flip();
        System.out.println("flip后："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        while (byteBuffer.hasRemaining()) {
            System.out.print((char) byteBuffer.get());
        }
        System.out.println("\n读完后："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        byteBuffer.rewind();
        System.out.println("rewind后："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        byteBuffer.clear();
        System.out.println("clear后："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
    }

    // 创建一个方法
    public static void duplicateTest() throws InterruptedException {
        // 用ByteBuffer分配一个直接缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        System.out.println("初始化："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        // 线程1根据byteBuffer 创建一个副本
        Thread thread1 = new Thread(() -> {
            ByteBuffer localBuffer = byteBuffer.duplicate(); // 创建副本
            localBuffer.put((byte) 1);
        });
        Thread thread2 = new Thread(() -> {
            ByteBuffer localBuffer = byteBuffer.duplicate(); // 创建副本
            localBuffer.put((byte) 2);
        });

        // 启动线程
        thread1.start();
        thread2.start();

        thread1.join(); // 等待线程1完成
        thread2.join(); // 等待线程2完成
        byteBuffer.flip();
        System.out.println("flip："+"position:" + byteBuffer.position() + ",limit:" + byteBuffer.limit());
        // 读取byte缓冲区中的数据
        while (byteBuffer.hasRemaining()) {
            System.out.print((char) byteBuffer.get());
        }
    }

    public static void duplicateTest2() {
        // 分配一个 ByteBuffer，容量为 10
        ByteBuffer originalBuffer = ByteBuffer.allocate(10);

        // 向原始缓冲区写入数据
        originalBuffer.put((byte) 1);
        originalBuffer.put((byte) 2);
        originalBuffer.put((byte) 3);

        // 打印原始缓冲区的信息
        System.out.println("Original Buffer before duplicate:");
        printBufferInfo(originalBuffer);

        // 创建一个原始缓冲区的副本
        ByteBuffer duplicateBuffer = originalBuffer.duplicate();

        // 向副本缓冲区写入数据
        duplicateBuffer.put((byte) 4);
        duplicateBuffer.put((byte) 5);

        // 打印原始缓冲区和副本缓冲区的信息
        System.out.println("Original Buffer after duplicate:");
        printBufferInfo(originalBuffer);
        System.out.println("Duplicate Buffer:");
        printBufferInfo(duplicateBuffer);

        // 重置原始缓冲区的位置为 0，以便读取
        originalBuffer.position(0);
        // 读取原始缓冲区的数据
        System.out.print("Original Buffer data: ");
        while (originalBuffer.hasRemaining()) {
            System.out.print(originalBuffer.get() + " ");
        }
        System.out.println();

        // 重置副本缓冲区的位置为 0，以便读取
        duplicateBuffer.position(0);
        // 读取副本缓冲区的数据
        System.out.print("Duplicate Buffer data: ");
        while (duplicateBuffer.hasRemaining()) {
            System.out.print(duplicateBuffer.get() + " ");
        }
        System.out.println();
    }


    // 打印缓冲区的信息的辅助方法
    public static void printBufferInfo(ByteBuffer buffer) {
        System.out.println("Position: " + buffer.position());
        System.out.println("Limit: " + buffer.limit());
        System.out.println("Capacity: " + buffer.capacity());
    }

}
